mui.init();

mui.plusReady(function(){

	/**  获取手机验证码 */
	var sendPhone =CmwMcore.$('sendPhone');
	MeventMgr.onTouchstart(sendPhone,function(event,self){
		var userId = plus.webview.currentWebview().userId;
		var dis=$("#sendPhone").attr("disabled")//判断是否有这属性
		if(dis)return; 
		$("#sendPhone").html("短信发送中...");
		$("#sendPhone").css('opacity',0.5);
		$("#sendPhone").prop("disabled", true); 
		if(!userId){
			userId = plus.storage.getItem(KeyCfg.userId);
		}
		var url =UrlConfig.msms_index;
		var smstype = $("#smsType").val();
		var pars = {smstype:smstype,userId:userId}
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var result = data.msg;
		  		sms_callback(result);	        		
		  	},ffn : function(){
		  		mui.alert("获取数据失败");
		  	}
		});	
	});
	
	/*------ 邮箱验证码  ------*/
	var sendEmail =CmwMcore.$('sendEmail');
	MeventMgr.onTouchstart(sendEmail,function(event,self){
		var dis=$("#sendEmail").attr("disabled")//判断是否有这属性
		if(dis)return;
		$("#sendEmail").html("邮件发送中...");
		$("#sendEmail").css('opacity',0.5);
		$("#sendEmail").prop("disabled", true); 
		var url =UrlConfig.sendemailcode;
		MuiEventMgr.ajax(url,{pars:{},
		  	sfn : function(data){
		  		var result = data.result;
		  		if (result == 1) {
		  			mui.alert("邮件验证码发送成功！");
					funemail_timedownn(120);
				} else if (result == 0) {
					mui.alert("邮件验证码发送失败！");
					$("#sendEmail").html("重新获取");
					$("#sendEmail").css('opacity',1);
					$("#sendEmail").prop("disabled",false); 
				} else if(result == -1){
					mui.alert("邮箱未绑定，请使用其他方式重置密码！");
					$("#sendEmail").html("获取邮箱验证码");
					$("#sendEmail").css('opacity',1);
					$("#sendEmail").prop("disabled",false); 
				}
		  	},ffn : function(eer){
		  		mui.alert("获取数据失败");
		  	}
		});
	});
});


function sms_callback(msg){
	if(msg==1){
		mui.alert("短信发送成功！");
		//$("#sendPhone").prop("disabled", true); 
		funphone_timedownn(120);
		//$('#sendsms').attr('href','javascript:void(0)');
	}else if(msg == 0){
		$("#sendPhone").html("重新获取");
		$("#sendPhone").prop("disabled", false); 
		$("#sendPhone").css('opacity',1);
		mui.alert("短信发送失败");
	}else if(msg==2){
		$("#sendPhone").html("重新获取");
		$("#sendPhone").prop("disabled", false); 
		$("#sendPhone").css('opacity',1);
		mui.alert("发送频率过高，2分钟之内不能重复发送");
	}else if(msg==3){
		$("#sendPhone").html("重新获取");
		$("#sendPhone").prop("disabled", false);
		$("#sendPhone").css('opacity',1);
		mui.alert("没有要发送的号码");
	}else if(msg==4){
		$("#sendPhone").html("重新获取");
		$("#sendPhone").prop("disabled", false); 
		$("#sendPhone").css('opacity',1);
		mui.alert("您还没有登录呢，不能发送短信");
	}
}


function funphone_timedownn(time)  {
    if(!time && time !== 0) time = 120;	        	
	$("#sendPhone").html(time+"秒");	    
    time = time-1;
    if(time>=0){
        setTimeout("funphone_timedownn("+time+")",1000);
    }else {
    	$("#sendPhone").html("重新获取");
    	$("#sendPhone").css('opacity',1);
    	$("#sendPhone").prop("disabled", false);
    }
}

function funemail_timedownn(time)  {
    if(!time && time !== 0) time = 120;	        	
	$("#sendEmail").html(time+"秒");	    
    time = time-1;
    if(time>=0){
        setTimeout("funemail_timedownn("+time+")",1000);
    }else {
    	$("#sendEmail").html("重新获取");
    	$("#sendEmail").css('opacity',1);
    	$("#sendEmail").prop("disabled", false);
    }
}
